<?php

namespace Modules\Code\Entities;

use Modules\Core\Entities\BaseEntity as Model;

use Modules\Card\Entities\Card;
use Modules\Product\Entities\Sku;

use ThrowException;

class Code extends Model
{
    protected $guarded = [];
    protected $table = 'codes';

    static public $resourceName = '兑换码';

    public function coupon()
    {
    	return $this->belongsTo('\Modules\Coupon\Entities\Coupon', 'coupon_id');
    }

    public function chip()
    {
        return $this->belongsTo('\Modules\Chip\Entities\Chip', 'chip_id');
    }

    public function records()
    {
    	return $this->hasMany('\Modules\Code\Entities\CodeRecord', 'code_id');
    }

    static function beforeGetList($options) 
    {
        if (@$options['title']) {
            $options['where']['title'] = $options['title'];
        }
        if (@$options['batch_number']) {
            $options['where']['batch_number'] = $options['batch_number'];
        }

        return $options;
    }

    public function use($user)
    {
        $this->status === 0 && ThrowException::Conflict('兑换码已下架~');
        $this->stock <= 0 && ThrowException::Conflict('此码兑换次数已用尽~');

        if ($this->records()->where('user_id', $user->id)->first()) {
        	ThrowException::Conflict('您已兑换过此码了哦~');
        }


        // 如果是积分
        if ($this->code_type == 'score') {
        	$user->asset('score')->in($this->score, [
        		'description' => '兑换码兑换'
        	]);
        }
        elseif ($this->code_type == 'redpack') {
        	$user->asset('redpack')->in($this->redpack, [
        		'description' => '兑换码兑换'
        	]);
        }
        elseif ($this->code_type == 'coupon') {
            if (!$this->coupon) {
                ThrowException::Conflict('优惠券不存在~');
            }

            $this->coupon->pushTo($user);
        }
        elseif ($this->code_type == 'exclude_card') {
            // 排除卡
            if ($this->prize_total) {
                $card = Card::getCardByType('exclude_box');
                $card->sendToUser($user, $this->prize_total, [
                    'description' => '兑换码兑换'
                ]);
            }
        }
        elseif ($this->code_type == 'show_card') {
            // 排除卡
            if ($this->prize_total) {
                $card = Card::getCardByType('show_box');
                $card->sendToUser($user, $this->prize_total, [
                    'description' => '兑换码兑换'
                ]);
            }
        }
        elseif ($this->code_type == 'chip') {
            // 兑换碎片
            if ($this->chip && $this->prize_total) {
                $this->chip->sendToUser($user, $this->prize_total, [
                    'description' => '兑换码兑换'
                ]);
            }
        }
        elseif ($this->code_type == 'product') {
            // 兑换碎片
            $sku = Sku::getEntity([
                'id' => $this->sku_id,
                'with' => ['product'],
            ], false) ?: ThrowException::Conflict('兑换的商品不存在~');

            // 赠送到用户盒柜
            $sku->sendAsPackageSku($user);
        }


        $this->records()->create([
        	'uuid' => uniqid(),
        	'user_id' => $user->id
        ]);

        $this->update([
        	'sales' => $this->sales + 1,
        	'stock' => $this->stock - 1
        ]);
    }

    public function checkUse($user) 
    {
    	return $this->records()->where('user_id', $user->id)->first();
    }
}
